<template>
  <div class="login_container">
      <div class="login_box">
          <!-- 头部区域 -->
          <div class="avatar_box">
            <img src="../assets/css/login.jpeg" alt="">
          </div>
          <!-- 表单区域 -->
          <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" class="login_form">
            <el-form-item prop="userName">
                <el-input v-model="loginForm.userName" prefix-icon="icon-user-solid-circle" placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="loginForm.password" prefix-icon="icon-lock-closed" placeholder="请输入密码" show-password></el-input>
            </el-form-item>
            <el-form-item class="form_item btns">
                <el-button type="primary" @click="login">登录</el-button>
            <el-button @click="resetLoginFrom">重置</el-button>
            </el-form-item>
           </el-form>
      </div>
  </div>
</template>

<script>
    export default {
        name: 'Login',
        data () {
            return {
                // 表单登录信息
                loginForm: {
                    userName: '',
                    password: ''
                },
                // 表单验证规则
                loginFormRules: {
                    // 验证用户名是否合法
                    userName: [
                        { required: true, message: '请输入账号', trigger: 'blur' },
                        { min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ],
                    // 验证密码是否合法
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            resetLoginFrom () {
                this.$refs.loginFormRef.resetFields()
            },
            login () {
                this.$refs.loginFormRef.validate(async valid => {
                    if (!valid) return 0
                    const { data: res } = await this.$http.post('login', this.loginForm)
                    console.log(res)
                    if (res.meta.status !== 200) return console.log('登录失败')
                    console.log('登录成功')
                })
            }
        }
    }
</script>

<style lang="css" scoped>
.login_container{
    display: flex;
    justify-content: center;
    align-items: center;
    background-color:  #2b4b6b;
    height: 100%;
}
.login_box{
    position: relative;
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 10px;
}
.avatar_box{
    position: absolute;
    width: 100px;
    width: 100px;
    left: 50%;
    transform: translate( -50% , -40%);
    border: 1px solid #eee;
    border-radius: 50%;
    background: url(../assets/css/login.jpeg) no-repeat;
    box-shadow: 0 0 10px #ddd;
    overflow: hidden;
}
.avatar_box img{
    width: 100%;
    height: 100%;
}
.login_form{
    position: absolute;
    bottom: 0;
    width: 100%;
    box-sizing: border-box;
    padding: 0 20px;
}
.form_item{
    display: flex;
    justify-content: flex-end;
     margin-top: 30px;
}
.btns{
    margin-bottom:30px;
}
</style>
